<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31
 * Time: 17:21
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\lib;

class Str
{
    /**
     * 生成唯一订单号
     * @return string
     */
    public static function Random(): string
    {
        return date('YmdHis') . substr(microtime(), 2, 5) . mt_rand(10000, 99999);
    }

    /**
     * @return string
     */
    public static function RandomStr(): string
    {
        return date('YmdHis') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * 生成唯一随机数
     * @param int $num
     * @return string
     */
    public static function RandStr($num = 8): string
    {
        $strand = time() . (double)microtime() * 1000000;
        if (strlen($strand) < $num) {
            $strand = md5(str_pad($strand, $num, "0", STR_PAD_LEFT) . time());
        }
        if ($num > 30) {
            $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $str_rand = '';
            for ($i = $num; $i > 0; $i--) {
                $str_rand .= $char[mt_rand(0, strlen($char) - 1)];
            }
            $string = $strand . $str_rand;
        } else {
            $string = $strand;
        }
        return mb_substr($string, 0, $num);
    }
}